<?php

namespace App\Http\Controllers\Api;

use App\Models\Book;
use App\Models\BookStockRemindUser;
use App\Models\Cart;
use App\Models\ExpressFeeConfig;
use Illuminate\Http\Request;

class CartController extends BaseController
{
    public function store(Request $request)
    {
        $book_id = intval($request->get('book_id'));
        $type = intval($request->get('type',1));  //1全新书 2良好书 3中等书

        if(empty($book_id) || empty($type) || !in_array($type,[1,2,3,4,5,6,7,8]))
        {
            return $this->err(1,200,'','参数错误');
        }

        $book = Book::query()->select(['new_book_stock','good_book_stock','medium_book_stock','total_stock','deleted','state','medium_book_stock2','medium_book_stock3','medium_book_stock4','medium_book_stock5','medium_book_stock6'])->where('id',$book_id)->first();
        if(empty($book) || $book->deleted || $book->state == 2)
        {
            return $this->err(1,200,'','图书不存在');
        }

        if($book->total_stock <= 0)
        {
            return $this->err(1,200,'','库存不足');
        }

        switch ($type)
        {
            case 1:
                if($book->new_book_stock <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 2:
                if($book->good_book_stock <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 3:
                if($book->medium_book_stock <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 4:
                if($book->medium_book_stock2 <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 5:
                if($book->medium_book_stock3 <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 6:
                if($book->medium_book_stock4 <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 7:
                if($book->medium_book_stock5 <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
            case 8:
                if($book->medium_book_stock6 <= 0)
                {
                    return $this->err(1,200,'','库存不足');
                }
                break;
        }

        $row = Cart::query()->where('user_id',$this->user_id)->where('book_id',$book_id)->first();
        if(!empty($row))
        {
            $row->update([
                'type' => $type
            ]);
        }
        else
        {
            Cart::create([
                'user_id' => $this->user_id,
                'book_id' => $book_id,
                'type'    => $type
            ]);
        }

        return $this->ok();
    }

    public function show(Request $request)
    {
        $result['stock'] = [];
        $result['unstock'] = [];
        $list = Cart::query()->with(['book'])->where('user_id',$this->user_id)->orderBy('id','desc')->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                //判断图书是否已经下架
                if($v->book->state == 2 || $v->book->deleted == 1)
                {
                    Cart::destroy($v->id);
                    continue;
                }

                //判断是否设置到货提醒
                $remind = BookStockRemindUser::query()->where('book_id',$v->book->id)->where('user_id',$this->user_id)->first();
                $is_setting_remind = 0;
                if(!empty($remind))
                {
                    $is_setting_remind = 1;
                }

                $stock_arr = [];
                $discount_arr = [];
                $price_arr = [];

                if($v->book->new_book_stock > 0)
                {
                    $stock_arr[] = $v->book->new_book_stock;
                    $discount_arr[] = $v->book->new_book_discount;
                    $price_arr[] = $v->book->new_book_price;
                }
                if($v->book->good_book_stock > 0)
                {
                    $stock_arr[] = $v->book->good_book_stock;
                    $discount_arr[] = $v->book->good_book_discount;
                    $price_arr[] = $v->book->good_book_price;
                }
                if($v->book->medium_book_stock > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock;
                    $discount_arr[] = $v->book->medium_book_discount;
                    $price_arr[] = $v->book->medium_book_price;
                }
                if($v->book->medium_book_stock2 > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock2;
                    $discount_arr[] = $v->book->medium_book_discount2;
                    $price_arr[] = $v->book->medium_book_price2;
                }
                if($v->book->medium_book_stock3 > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock3;
                    $discount_arr[] = $v->book->medium_book_discount3;
                    $price_arr[] = $v->book->medium_book_price3;
                }
                if($v->book->medium_book_stock4 > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock4;
                    $discount_arr[] = $v->book->medium_book_discount4;
                    $price_arr[] = $v->book->medium_book_price4;
                }
                if($v->book->medium_book_stock5 > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock5;
                    $discount_arr[] = $v->book->medium_book_discount5;
                    $price_arr[] = $v->book->medium_book_price5;
                }
                if($v->book->medium_book_stock6 > 0)
                {
                    $stock_arr[] = $v->book->medium_book_stock6;
                    $discount_arr[] = $v->book->medium_book_discount6;
                    $price_arr[] = $v->book->medium_book_price6;
                }

                $is_more = 0;
                if(count($stock_arr) > 1)
                {
                    $is_more = 1;
                }

                $min_discount = 0;
                $min_price = 0.0;
                if(!empty($discount_arr))
                {
                    $min_discount = min($discount_arr);
                }
                if(!empty($price_arr))
                {
                    $min_price = min($price_arr);
                }

                $item = [
                    'id'                        => $v->id,
                    'type'                      => $v->type,
                    'is_selected'               => $v->is_selected,
                    'title'                     => $v->book->title,
                    'book_id'                   => $v->book->id,
                    'image'                     => $v->book->image,
                    'total_stock'               => $v->book->total_stock,
                    'new_book_price'            => $v->book->new_book_price,
                    'new_book_stock'            => $v->book->new_book_stock,
                    'good_book_price'           => $v->book->good_book_price,
                    'good_book_stock'           => $v->book->good_book_stock,
                    'medium_book_price'         => $v->book->medium_book_price,
                    'medium_book_stock'         => $v->book->medium_book_stock,
                    'old_price'                 => $v->book->old_price,
                    'new_book_discount'         => $v->book->new_book_discount,
                    'good_book_discount'        => $v->book->good_book_discount,
                    'medium_book_discount'      => $v->book->medium_book_discount,
                    'is_setting_remind'         => $is_setting_remind,
                    'medium_book_price2'        => $v->book->medium_book_price2,
                    'medium_book_stock2'        => $v->book->medium_book_stock2,
                    'medium_book_discount2'     => $v->book->medium_book_discount2,
                    'medium_book_price3'        => $v->book->medium_book_price3,
                    'medium_book_stock3'        => $v->book->medium_book_stock3,
                    'medium_book_discount3'     => $v->book->medium_book_discount3,
                    'medium_book_price4'        => $v->book->medium_book_price4,
                    'medium_book_stock4'        => $v->book->medium_book_stock4,
                    'medium_book_discount4'     => $v->book->medium_book_discount4,
                    'medium_book_price5'        => $v->book->medium_book_price5,
                    'medium_book_stock5'        => $v->book->medium_book_stock5,
                    'medium_book_discount5'     => $v->book->medium_book_discount5,
                    'medium_book_price6'        => $v->book->medium_book_price6,
                    'medium_book_stock6'        => $v->book->medium_book_stock6,
                    'medium_book_discount6'     => $v->book->medium_book_discount6,
                    'is_more'                   => $is_more,
                    'min_discount'              => $min_discount,
                    'min_price'                 => $min_price,
                ];
                if($v->book->total_stock > 0)
                {
                    $result['stock'][] = $item;
                }
                else
                {
                    $result['unstock'][] = $item;
                }
            }
        }

        //获取运费设置
        $unfar_express_fee = 0;
        $unfar_order_price = 0;
        $unfar_info = ExpressFeeConfig::query()->where('type',1)->first();
        if(!empty($unfar_info))
        {
            $unfar_express_fee = $unfar_info->express_fee;
            $unfar_order_price = $unfar_info->order_price;
        }

        $result['express'] = ['unfar_express_fee' => $unfar_express_fee, 'unfar_order_price' => $unfar_order_price];
        return $this->ok($result);
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $cart = Cart::find($id);
        if(empty($cart))
        {
            return $this->err(1,200,'','删除失败');
        }

        if($cart->user_id != $this->user_id)
        {
            return $this->err(1,200,'','删除失败');
        }
        Cart::destroy($id);
        return $this->ok();
    }

    public function selected(Request $request)
    {
        $id             = intval($request->get('id'));
        $is_selected    = intval($request->get('is_selected'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $cart = Cart::find($id);
        if(empty($cart))
        {
            return $this->err(1,200,'','操作失败');
        }

        if($is_selected <= 0)
        {
            $is_selected = 0;
        }
        else
        {
            $is_selected = 1;
        }
        $cart->update(['is_selected' => $is_selected]);
        return $this->ok();
    }
}
